package redis.work;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import redis.util.CookieStore;

/**
 * @author houweitao
 * @date 2016年5月17日下午10:34:48
 */

public class LightUp {
	private static final Logger log = LoggerFactory.getLogger(LightUp.class);

	private static final String light = "http://bbs.hupu.com/ajax/lights.ajax.php";
	private static final Map<String, String> cookies = CookieStore.getCookies();

	public static void main(String[] args) {

		LightUp lightUp = new LightUp();
		lightUp.lightSingle("http://bbs.hupu.com/16274018.html");
	}

	void lightSingle(String url) {
		
//		Map<String,String> addCookie=CookieStore.getCookies("lastvisit=293417%091463535847%09%2Fajax%2Flights.ajax.php%3F; expires=Sun, 17-Jul-2016 01:44:07 GMT; path=/; domain=bbs.hupu.com");
//		cookies.putAll(addCookie);
		
		
		String help = url.split("com")[1];
		String tid = help.substring(1, help.length() - 5);

		try {
			Document doc = Jsoup.connect(url).followRedirects(true).timeout(10000)
					.userAgent(
							"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36")
					.get();

			String fid = doc.select(".bbs-hd-h1").select("h1#j_data").first().attr("fid");
			System.out.println("fid: " + fid);

			Elements answers = doc.select(".floor").select(".author").select(".left");

			System.out.println(answers.size());

			for (Element answer : answers) {
				String pid = answer.select(".f444").attr("pid");
				String authorid = answer.select(".f444").attr("uid");
				String token = answer.select(".f444").attr("token");

				System.out.println("=====" + tid + "," + fid + "," + pid + "," + authorid + "," + token);

				Map<String, String> data = buildData(fid, tid, pid, authorid, token);
				Document result = Jsoup.connect(light).followRedirects(true).timeout(10000)
						.userAgent(
								"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36")
						.cookies(cookies).data(data).post();

				System.out.println(result.select("body").text());

				LightResult lr = JSON.parseObject(result.select("body").text(), LightResult.class);
				if (lr.getCode() > 0)
					log.info("点亮成功");
				else
					log.info("点亮失败");
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private Map<String, String> buildData(String fid, String tid, String pid, String authorid, String token) {
		// tid:16274433
		// pid:401697
		// state:1 亮，2 灭
		// authorid:19020350
		// fid:85
		// token:56a0940f6ac6a862e584aef6f84a0bb2

		Map<String, String> params = new HashMap<String, String>();
		params.put("fid", fid);
		params.put("pid", pid);
		params.put("authorid", authorid);
		params.put("tid", tid);
		params.put("token", token);
		params.put("state", "1");

		return params;

	}
}

class LightResult {
	private int code;
	private String msg;

	public LightResult() {

	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}